package com.todo.pro.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.todo.pro.entity.SystemLoginPic;
import com.todo.pro.handle.EE;
import com.todo.pro.jwt.JwtUtil;
import com.todo.pro.res.JsonResult;
import com.todo.pro.res.model.SystemLoginPicResult;
import com.todo.pro.service.SystemLoginPicService;
import com.todo.pro.util.MapUtil;
import com.xiaoleilu.hutool.date.DateUtil;
import com.xiaoleilu.hutool.util.NumberUtil;

/**
 * @author SiZhongxia
 * @since 2017-12-27
 */
@Controller
@RequestMapping("loginPic")
public class LoginPicController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(LoginPicController.class);

	@Autowired
	private SystemLoginPicService systemLoginPicService;

	@RequiresAuthentication
	@RequestMapping(value = "list")
	public ModelAndView list(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("modules/loginPic/list");
		mav.addObject("csrftoken",
				JwtUtil.createToken(MapUtil.init().putVal("path", "gq-loginPic-list"), 2L * 60 * 60 * 1000));
		return mav;
	}

	@ResponseBody
	@RequiresAuthentication
	@RequestMapping(value = "data")
	public JsonResult<List<SystemLoginPicResult>> data(HttpServletRequest request) {
		Wrapper<SystemLoginPic> loginPicWrapper = new EntityWrapper<>();

		String keyword = request.getParameter("keyword");
		if (StringUtils.isNotBlank(keyword)) {
			loginPicWrapper.like("title", keyword);
		}

		loginPicWrapper.orderBy("used", false);
		List<SystemLoginPic> loginPics = systemLoginPicService.selectList(loginPicWrapper);

		List<SystemLoginPicResult> loginPicrs = new ArrayList<>();
		int count = loginPics.size();
		if (count > 0) {
			for (SystemLoginPic loginPic : loginPics) {
				SystemLoginPicResult loginPicr = new SystemLoginPicResult();
				BeanUtils.copyProperties(loginPic, loginPicr);
				loginPicr.setId(loginPic.getId().toString());
				loginPicrs.add(loginPicr);
			}
		}
		return new JsonResult<List<SystemLoginPicResult>>(count, loginPicrs);
	}

	@ResponseBody
	@RequiresAuthentication
	@RequestMapping(value = "changeusedstatus")
	public JsonResult<Object> changeusedstatus(HttpServletRequest request) {
		String id = request.getParameter("id");
		if (StringUtils.isBlank(id)) {
			return new JsonResult<Object>(false, EE.INVALID_PARAMETERS.i());
		}
		String used = request.getParameter("used");
		if (!NumberUtil.isInteger(used)) {
			return new JsonResult<Object>(false, EE.INVALID_PARAMETERS.i());
		}

		SystemLoginPic systemLoginPic = systemLoginPicService.selectById(id);
		if (systemLoginPic == null) {
			return new JsonResult<Object>(false, EE.INVALID_PARAMETERS.i());
		}
		systemLoginPic.setUsed(new Integer(used));
		systemLoginPic.setUpdateTime(DateUtil.date());
		if (!systemLoginPicService.updateById(systemLoginPic)) {
			return new JsonResult<Object>(false, EE.ERR_FOR_WAIT.i());
		}

		return new JsonResult<Object>(true, EE.SUCCESS_OP.i());
	}

	@ResponseBody
	@RequiresAuthentication
	@RequestMapping(value = "updateField")
	public JsonResult<Object> updateField(HttpServletRequest request) {
		String csrftoken = request.getParameter("csrftoken");
		if (StringUtils.isBlank(csrftoken)) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}
		Map<String, Object> jwtData = JwtUtil.parseJWT(csrftoken);
		if (jwtData == null) {
			return new JsonResult<Object>(false, EE.ERROR_FORM_OVERDUE.i());
		}

		if (!"gq-loginPic-list".equals(jwtData.get("path"))) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}

		String id = request.getParameter("id");
		if (StringUtils.isBlank(id)) {
			return new JsonResult<Object>(false, EE.INVALID_PARAMETERS.i());
		}

		String field = request.getParameter("field");
		if (StringUtils.isBlank(field)) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}

		String value = request.getParameter("value");
		if (StringUtils.isBlank(value)) {
			value = "";
		}

		SystemLoginPic systemLoginPic = systemLoginPicService.selectById(id);
		if (systemLoginPic == null) {
			return new JsonResult<Object>(false, EE.INVALID_PARAMETERS.i());
		}
		if ("title".equals(field)) {
			systemLoginPic.setTitle(value);
		} else if ("url".equals(field)) {
			systemLoginPic.setUrl(value);
		} else {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}
		systemLoginPic.setUpdateTime(DateUtil.date());
		if (!systemLoginPicService.updateById(systemLoginPic)) {
			return new JsonResult<Object>(false, EE.ERR_FOR_WAIT.i());
		}

		return new JsonResult<Object>(true, EE.SUCCESS_OP.i());
	}

	@RequiresAuthentication
	@RequestMapping(value = "add")
	public ModelAndView add(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("modules/loginPic/add");
		mav.addObject("csrftoken",
				JwtUtil.createToken(MapUtil.init().putVal("path", "gq-loginPic-add"), 30L * 60 * 1000));
		return mav;
	}

	@ResponseBody
	@RequiresAuthentication
	@RequestMapping(value = "save")
	public JsonResult<Object> save(HttpServletRequest request) {
		String csrftoken = request.getParameter("csrftoken");
		if (StringUtils.isBlank(csrftoken)) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}
		Map<String, Object> jwtData = JwtUtil.parseJWT(csrftoken);
		if (jwtData == null) {
			return new JsonResult<Object>(false, EE.ERROR_FORM_OVERDUE.i());
		}

		if (!"gq-loginPic-add".equals(jwtData.get("path"))) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}
		String title = request.getParameter("title");
		if (StringUtils.isBlank(title)) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}

		String url = request.getParameter("url");
		if (StringUtils.isBlank(url)) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}

		SystemLoginPic systemLoginPic = new SystemLoginPic();
		systemLoginPic.setTitle(title);
		systemLoginPic.setUrl(url);
		systemLoginPic.setUsed(0);
		systemLoginPic.setCreateTime(DateUtil.date());
		systemLoginPic.setUpdateTime(DateUtil.date());
		if (!systemLoginPicService.insert(systemLoginPic)) {
			return new JsonResult<Object>(false, EE.ERR_FOR_WAIT.i());
		}

		return new JsonResult<Object>(true, EE.SUCCESS_OP.i());
	}

	@ResponseBody
	@RequiresAuthentication
	@RequestMapping(value = "delete")
	public JsonResult<Object> delete(HttpServletRequest request) {
		String csrftoken = request.getParameter("csrftoken");
		if (StringUtils.isBlank(csrftoken)) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}
		Map<String, Object> jwtData = JwtUtil.parseJWT(csrftoken);
		if (jwtData == null) {
			return new JsonResult<Object>(false, EE.ERROR_FORM_OVERDUE.i());
		}

		if (!"gq-loginPic-list".equals(jwtData.get("path"))) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}

		String[] ids = request.getParameterValues("ids[]");
		if (ids == null || ids.length == 0) {
			return new JsonResult<Object>(false, EE.SELECT_LEAST_ONE_RECORD.i());
		}

		Set<Long> deleteLoginPicIds = new HashSet<>();
		for (String loginPicId : ids) {
			deleteLoginPicIds.add(new Long(loginPicId));
		}

		if (!systemLoginPicService.deleteBatchIds(deleteLoginPicIds)) {
			return new JsonResult<Object>(false, EE.ERR_FOR_WAIT.i());
		}

		return new JsonResult<Object>(true, EE.SUCCESS_OP.i());
	}

}
